// pages/business/live-lists/live-lists.js
var app = getApp()
var util = require('.../../../../../utils/util')
Page({

  /**
   * 页面的初始数据
   */
  data: {
    living:[],
    beforeLive:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    this.getLists();
  },
  getLists(){
    var that = this;
    wx.request({
      url: app.globalData.urls + '/live/room-list',
      method: 'POST',
      data: {
        
      },
      header: app.getPostRequestHeader(),
      success: function (res) {
        if (res.data.code == '200') {
          let data = res.data.data,getdate=(new Date().getTime())/1000;
          console.log("---data",data)
          data = data.filter(val=>{
            if(that.data.allData[val.roomid]){
              return val;
            }
          })
          console.log("-----",data)
          let living = data.filter(val=>{
            val.stime = util.formatDateTime(val.start_time*1000)
            // val.live_status==101||val.live_status==102
            return val.live_status==101
          }).sort(a=>getdate - a.start_time)
          let waiting = data.filter(val=>{
            val.stime = util.formatDateTime(val.start_time*1000)
            // val.live_status==101||val.live_status==102
            return val.live_status==102
          }).sort(a=>getdate - a.start_time)
          that.setData({
            living:living.concat(waiting),
            beforeLive:data.filter(val=>{
              val.stime = util.formatDateTime(val.start_time*1000)
              return val.live_status==103
            })
          })
        }
      }
    })
  },
  // 获取历史播放
  beforeLive(e){
    let getItem=e.currentTarget.dataset.item;
    wx.navigateTo({
      // &custom_params=${customParams}
      url: `plugin-private://wx2b03c6e691cd7370/pages/live-player-plugin?room_id=`+getItem.roomid
    })
  },
  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    this.getAll();
   
  },
  getAll(){
    var that = this;
    wx.request({
      url: app.globalData.urls + '/live/live-record',
      method: 'POST',
      data: {
        bdmin_user_id:util.share_bdmin_user_id(),
      },
      header: app.getPostRequestHeader(),
      success: function (res) {
        if (res.data.code == '200') {
          let data=res.data.data
        let result = data.reduce((prev,val)=>{
          prev[val.roomid]=1;
          return prev
        },{})
        console.log(result,'=====')
          that.setData({
            allData:result
          })
          console.log("----",res.data.data)
        }
      }
    })
    
  },
  // 正在直播
  openLive(e){
    console.log(e.currentTarget.dataset)
    let roomId = e.currentTarget.dataset.item.roomid;
    //let roomId = [直播房间id] // 填写具体的房间号，可通过下面【获取直播房间列表】 API 获取
    // let customParams = encodeURIComponent(JSON.stringify({ path: 'pages/business/live-lists/live-lists', pid: 1 })) // 开发者在直播间页面路径上携带自定义参数（如示例中的path和pid参数），后续可以在分享卡片链接和跳转至商详页时获取，详见【获取自定义参数】、【直播间到商详页面携带参数】章节（上限600个字符，超过部分会被截断）
    wx.navigateTo({
      // &custom_params=${customParams}
      url: `plugin-private://wx2b03c6e691cd7370/pages/live-player-plugin?room_id=${roomId}`
    })
  },
})